package com.nondo.rdp.finance.repository.course;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.nondo.rdp.core.common.StatusConstant;
import com.nondo.rdp.finance.domain.course.CourseRate;

public interface CourseRateRepository extends JpaRepository<CourseRate, Long>,
		JpaSpecificationExecutor<CourseRate> {
	@Query("select a from CourseRate a where a.id=:id and delFlag = '" + StatusConstant.DEL_FLAG_NORMAL + "'")
	Optional<CourseRate> findOne(@Param("id") int id);

	@Modifying
	@Query("update CourseRate set delFlag='" + StatusConstant.DEL_FLAG_DELETE + "' where id = ?1")
	void deleteById(Long id);

	CourseRate save(CourseRate CourseRate);

	@Query("select c from CourseRate c where c.course.id=:cid and c.rateUser.id=:uid and delFlag='" + StatusConstant.DEL_FLAG_NORMAL + "'")
	CourseRate findCourseRateByCourseAndUser(@Param("cid") long cid, @Param("uid") long uid);

	@Query("select AVG(a.rate) from CourseRate a where a.course.id=? and delFlag = '" + StatusConstant.DEL_FLAG_NORMAL + "'")
	Float getAvgRate(Long cid);

}
